<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            看板统计
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">要货日期起</div>
                    <div class="aui-list-item-input" onclick="timePicker('needDateStart');">
                        <input type="text" placeholder="选择日期" class="aui-input" id="needDateStart">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">要货日期止</div>
                    <div class="aui-list-item-input" onclick="timePicker('needDateEnd');">
                        <input type="text" placeholder="选择日期" class="aui-input" id="needDateEnd">
                    </div>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="search()">查询</div>
    </div>
    <div id="echartPei" style="height: 200px;width: 100%;margin-top:20px;">
    </div>
    <div id="echartBar" style="height: 380px;width: 100%;">
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/echarts.min.js"></script>
<script type="text/javascript">
    //初始化
    apiready = function () {

        //初始化当前日期
        crateDate();
    }

    //获取当前日期
    function crateDate() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        $("#needDateStart").val(year + "-" + month + "-" + day);
        $("#needDateEnd").val(year + "-" + month + "-" + day);
        search();
    }

    //时间选择器
    function timePicker(id) {
        api.openPicker({
            type: 'date',
            date: new Date(),
            title: '选择日期'
        }, function (ret, err) {
            if (ret) {
                var monthTemp = (ret.month < 10 ? '0' + ret.month : ret.month);
                var dayTemp = (ret.day < 10 ? '0' + ret.day : ret.day);
                var date = ret.year + "-" + monthTemp + "-" + dayTemp;
                $("#" + id).val(date);
            }
        });
    }

    //查询看板信息
    function search() {

        //要货日期起
        var needDateStart = $("#needDateStart").val();
        //要货日期止
        var needDateEnd = $("#needDateEnd").val();
        if (needDateStart == null || needDateStart == "" || needDateStart == undefined) {
            api.toast({
                msg: "请选择要货日期起",
                duration: 3000
            });
            return;
        }
        if (needDateEnd == null || needDateEnd == "" || needDateEnd == undefined) {
            api.toast({
                msg: "请选择要货日期止",
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/searchKanbanListGroup";
        var data = {
            values: {
                needDateStart: needDateStart,
                needDateEnd: needDateEnd,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);

                    if (list.length == 0) {
                        $("#echartPei").html("");
                        $("#echartBar").html("");
                        api.toast({
                            msg: "无看板数据",
                            duration: 3000
                        });
                        return;
                    }

                    //1：构造饼图
                    createPei(list);

                    //2：构造柱状图
                    createBar(list);

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $api.html($api.byId("divhtml"), "");
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //饼图
    function createPei(list) {
        var yData = [];
        var colorFirst = ['#6495ED', '#48D1CC', '#00FA9A', '#FF69B4', '#9400D3', '#FFD700', '#FFA07A'];
        var colorList = [];
        //遍历
        for (var i = 0; i < list.length; i++) {
            var qty = list[i].qty;
            if (qty != null && qty != "" && parseInt(qty) > 0) {
                yData.push({
                    value: list[i].qty,
                    name: list[i].name
                });
                if (list[i].name == "创建") {
                    colorList.push(colorFirst[0]);
                } else if (list[i].name == "发布") {
                    colorList.push(colorFirst[1]);
                } else if (list[i].name == "接收") {
                    colorList.push(colorFirst[2]);
                } else if (list[i].name == "无法送达") {
                    colorList.push(colorFirst[3]);
                } else if (list[i].name == "备货") {
                    colorList.push(colorFirst[4]);
                } else if (list[i].name == "发货") {
                    colorList.push(colorFirst[5]);
                } else if (list[i].name == "完成") {
                    colorList.push(colorFirst[6]);
                }
            }
        }

        var myChart = echarts.init(document.getElementById('echartPei'));

        var option = {
            tooltip: {
                trigger: 'item',
                formatter: '{a} <br/>{b} : {c} ({d}%)'
            },
            grid: {
                left: '3%',
                right: '3%',
            },
            color: colorList,
            series: [{
                type: 'pie',
                radius: '55%',
                center: ['52%', '50%'],
                selectedMode: 'single',
                data: yData,
                label: {
                    normal: {
                        formatter: '{b}:({d}%)',
                        // textStyle: {　　　　　　　　
                        //     fontWeight: 'normal',
                        //     fontSize: 14　　　　　　
                        // }　　　　
                    }
                },
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }]

        };
        myChart.setOption(option, true);
    }

    //柱状图
    function createBar(list) {

        var xData = [];
        var yData = [];

        //遍历
        for (var i = 0; i < list.length; i++) {
            var qty = list[i].qty;
            if (qty != null && qty != "" && parseInt(qty) > 0) {
                xData.push(list[i].qty);
                yData.push(list[i].name);
            }
        }

        var myChart = echarts.init(document.getElementById('echartBar'));

        var option = {
            grid: {
                left: '3%',
                right: '8%',
                bottom: '1%',
                containLabel: true
            },
            xAxis: {
                // type: 'value',
                boundaryGap: [0, 0.01],
                show: false,
            },
            yAxis: [{
                type: 'category',
                data: yData,
                axisTick: {
                    show: false //不显示y轴刻度
                },
                splitLine: {
                    show: false
                },
                axisLine: { //x轴
                    show: false,
                },
                inverse: true,
            }, {
                type: 'category',
                data: xData,
                axisTick: {
                    show: false
                },
                splitLine: {
                    show: false
                },
                axisLine: { //x轴
                    show: false,
                },
                inverse: true,
            }],
            color: ['#00BFFF'],
            series: [{
                type: 'bar',
                yAxisIndex: 0,
                data: xData,
                // 柱子之间的宽度
                barWidth: 20,
                // 柱子之间的间距
                barCategoryGap: 5,
                // 修改第一条柱子的圆角
                itemStyle: {
                    normal: {
                        barBorderRadius: 20,
                    },
                },
                zlevel: 1,
            }, {
                name: '框',
                yAxisIndex: 1,
                type: 'bar',
                // data: [100, 100, 100, 100, 100],
                data: xData,
                // 柱子之间的间距
                barCategoryGap: 5,
                // 柱子之间的宽度
                barWidth: 20,
                itemStyle: {
                    normal: {
                        barBorderRadius: 20,
                    },
                }
            }]
        };
        myChart.setOption(option, true);

        //柱状图点击事件
        myChart.on('click', function (params) {
            var needDateStart = $("#needDateStart").val();
            var needDateEnd = $("#needDateEnd").val();
            var name = params.name;
            var kanbanStatus = "";
            if (name == "创建") {
                kanbanStatus = "1";
            } else if (name == "发布") {
                kanbanStatus = "2";
            } else if (name == "接收") {
                kanbanStatus = "3";
            } else if (name == "无法送达") {
                kanbanStatus = "4";
            } else if (name == "备货") {
                kanbanStatus = "5";
            } else if (name == "发货") {
                kanbanStatus = "6";
            } else if (name == "完成") {
                kanbanStatus = "7";
            }

            //开发验证详细页面
            api.openWin({
                name: 'ssKanbanData',
                url: 'ssKanbanData.html',
                pageParam: {
                    kanbanStatus: kanbanStatus,
                    needDateStart: needDateStart,
                    needDateEnd: needDateEnd
                }
            });
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>